// @ts-nocheck
import axios from "axios";
import type { AxiosInstance } from "axios";
// 导入自定义类型
import { METHOD } from "./config";
import type { RequestConfig, RequestInterceptors } from "./type";
const DEAFULT_LOADING = false; // 默认显示loading
const DEAFULT_ISRESPONSE = false; // 默认不响应

interface WdLayout<T> {
  code: number;
  data: T;
  success?: boolean;
  msg: string;
  [key: string]: any;
}

class HttpRequest {
  instance: AxiosInstance; // axios实例
  interceptors?: RequestInterceptors; // 拦截器
  showLoading: boolean; // 是否显示loading
  loading?: boolean; // 是否显示loading
  isResponse?: boolean; // 是否响应
  constructor(config: RequestConfig) {
    this.instance = axios.create(config);
    this.showLoading = config.showLoading ?? DEAFULT_LOADING;
    this.isResponse = config.isResponse ?? DEAFULT_ISRESPONSE;
    this.interceptors = config.interceptors;
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    );
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    );
    // 添加全局拦截器
    this.instance.interceptors.request.use(
      (config) => {
        return config;
      },
      (err) => {
        return err;
      }
    );
    // 添加全局响应器
    this.instance.interceptors.response.use(
      (response) => {
        if (response.config.isResponse) {
          return response;
        }
        return response.data;
      },
      (err) => {
        return err;
      }
    );
  }
  request<T>(config: RequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      // 单个请求对请求config的处理
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config);
      }
      // 判断是否需要显示loading
      if (config.showLoading) {
        this.showLoading = config.showLoading;
      }
      this.instance
        .request<any, T>(config)
        .then((response) => {
          // 单个请求对数据的处理
          if (config.interceptors?.responseInterceptor) {
            response = config.interceptors.responseInterceptor(
              response,
              config
            );
          }
          // 将showLoading设置true，这样不会影响下一个请求
          this.showLoading = DEAFULT_LOADING;
          // 把结果resolve返回出去
          resolve(response);
        })
        .catch((err) => {
          this.showLoading = DEAFULT_LOADING;
          reject(err);
          return err;
        });
    });
  }
  get<T = WdLayout<any>>(config: RequestConfig<T>): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.request<T>({ ...config, method: METHOD.GET })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
  post<T = WdLayout<any>>(config: RequestConfig<T>): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.request<T>({ ...config, method: METHOD.POST })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
  delete<T = WdLayout<any>>(config: RequestConfig<T>): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.request<T>({ ...config, method: METHOD.DELETE })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
  put<T = WdLayout<any>>(config: RequestConfig<T>): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.request<T>({ ...config, method: METHOD.PUT })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}

export default HttpRequest;
